package middleware

import (
	"common/logger"

	"net/http"

	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
)

var db *gorm.DB

func DbPrepareHandler(c *gin.Context) {
	if db == nil {
		var err error
		db, err = gorm.Open("mysql", "root:123456@tcp(127.0.0.1:3306)/test2?charset=utf8&parseTime=True&loc=Local")
		if err != nil {
			logger.Error("Unable to connect to db ", err)
			c.AbortWithStatus(http.StatusInternalServerError)
			return
		}
		db.LogMode(true)
	}
	c.Set("ty", db)
	c.Next()
}

